﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLogicLayer;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class PhanQuyenBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
        public List<CT_PHANQUYEN> LayTatCa()
        {
            return DB.CT_PHANQUYENs.ToList();
        }
        public List<string> layTatCaTrang()
        {
            return DB.CT_PHANQUYENs.Select(a => a.DuongDan).Distinct().ToList();
        }
        public string layRoleTheoTenTrang(string _DuongDan)
        {
            return DB.CT_PHANQUYENs.Where(a => a.DuongDan == _DuongDan).FirstOrDefault().MaRole.ToString();
        }

        //Từ đường dẫn của page, lấy danh sách role theo page đó, 2 cách
        //Cách 1
        public List<int> layDSRoleTheoTenTrang(string _DuongDan)
        {

            var r = from a in DB.CT_PHANQUYENs
                    where a.DuongDan == _DuongDan.ToLower()
                    select a.MaRole;
            List<int> lstRole = new List<int>();
            foreach (var item in r)
            {
                lstRole.Add(item ?? int.MaxValue);
            }
            return lstRole;
        }
        //Cách 2
        public List<int> layDSRoleTheoTrang(string _DuongDan)
        {
            List<int> lstRole = (from a in DB.CT_PHANQUYENs
                            where a.DuongDan == _DuongDan
                            select a.MaRole ?? int.MaxValue 
                          ).ToList();
            return lstRole;
        }

        public void themTrangMoi(CT_PHANQUYEN ctq)
        {
            DB.CT_PHANQUYENs.InsertOnSubmit(ctq);
            DB.SubmitChanges();
        }

        public void suaQuyenTrenTrang(CT_PHANQUYEN ctq)
        {
            CT_PHANQUYEN ctq2 = new CT_PHANQUYEN();// = DB.CT_PHANQUYENs.Single(a => a.DuongDan == ctq.DuongDan);
            ctq2.DuongDan = ctq.DuongDan;
            ctq2.MoTa = ctq.MoTa;
            ctq2.MaRole = ctq.MaRole;
            DB.CT_PHANQUYENs.InsertOnSubmit(ctq2);
            DB.SubmitChanges();
        }
        public void xoaQuyenTrenTrang(string _DuongDan, int _MaRole)
        {
            var q = (from pq in DB.CT_PHANQUYENs
                    where pq.DuongDan == _DuongDan && pq.MaRole == _MaRole
                    select pq);
            DB.CT_PHANQUYENs.DeleteAllOnSubmit(q);
            DB.SubmitChanges();
        }
        public void xoaPhanQuyenTrenTrang(string _DuongDan)
        {
            //CT_PHANQUYEN ctq = DB.CT_PHANQUYENs.Where(a => a.DuongDan == _DuongDan).FirstOrDefault();
            //DB.CT_PHANQUYENs.DeleteOnSubmit(ctq);
            //DB.SubmitChanges();
            CT_PHANQUYEN c = DB.CT_PHANQUYENs.Single(a => a.DuongDan == _DuongDan);
            DB.CT_PHANQUYENs.DeleteOnSubmit(c);
            DB.SubmitChanges();

            List<string> lstRole = (from a in DB.CT_PHANQUYENs
                                 where a.DuongDan == _DuongDan
                                 select a.DuongDan).ToList();

            foreach (var author in lstRole)
            {
                lstRole.Remove(author);
            }

            //List<int> lstRole = (from a in DB.CT_PHANQUYENs
            //                     where a.DuongDan == _DuongDan
            //                     select a.MaRole ?? int.MaxValue
            //  ).ToList();
            //return lstRole;


        }

        public CT_PHANQUYEN layThongTinPhanQuyenTrenTrang(string _DuongDan)
        {
            CT_PHANQUYEN ctq = DB.CT_PHANQUYENs.Where(a => a.DuongDan == _DuongDan).FirstOrDefault();
            return ctq;
        }
    }
}
